package com.example.test1;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import okhttp3.internal.http.RealResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.http.Body;
import retrofit2.http.GET;
import retrofit2.http.POST;

public class GuanWeiActivity extends Activity {

    private TextView fanhui;
    private GridView gridView;
    private List<Users> list;
    private myGridView myGridView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guan_wei);
        initView();
        initData();
        fanhui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(GuanWeiActivity.this, MainActivity.class));
            }
        });
    }

    private void getData(String m,String n) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://hh1.me:5001/")
                .build();
        Map<String,String> map =new HashMap<>();
        map.put("pos",m);
        map.put("gangwei",n);
        String s = new Gson().toJson(map);
        RequestBody requestBody= RequestBody.create(s,MediaType.parse("application/json;charset=utf-8"));
        BlogPsot blogServer = retrofit.create(BlogPsot.class);
        Call<ResponseBody> blog = blogServer.getBlog(requestBody);
        blog.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                initData();
                Toast.makeText(GuanWeiActivity.this,"替补成功",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {

            }
        });
    }

    private interface BlogServer {
        @GET("getProduceInfo")
        Call<ResponseBody> getBlog();
    }
    private interface BlogPsot{
        @POST("orderWorker")
        Call<ResponseBody> getBlog(@Body RequestBody str);
    }
    private void initData() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://hh1.me:5001/")
                .build();
        BlogServer blogServer = retrofit.create(BlogServer.class);
        Call<ResponseBody> blog = blogServer.getBlog();
        blog.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    list = new ArrayList<>();
                    String s = response.body().string();
                    Users users = new Gson().fromJson(s, Users.class);
                    list.add(users);
                    myGridView = new myGridView(list);
                    gridView.setAdapter(myGridView);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {

            }
        });
    }

    private void initView() {
        fanhui = (TextView) findViewById(R.id.fanhui);
        gridView = (GridView) findViewById(R.id.gridView);
    }

    private class myGridView extends BaseAdapter {
        List<Users> list;

        public myGridView(List<Users> list) {
            this.list = list;
        }

        @Override
        public int getCount() {
            return list.get(0).getData().size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }
        private class ViewHolder{
            TextView weizhiText;
            TextView gcsText;
            TextView jsyText;
            TextView czyText;
            TextView zjyText;
        }
        @Override
        public View getView(final int i, View view, ViewGroup viewGroup) {
            ViewHolder holder;
            if(view==null) {
                view = View.inflate(GuanWeiActivity.this, R.layout.layout, null);
                holder = new ViewHolder();
                holder.weizhiText = (TextView) view.findViewById(R.id.weizhiText);
                holder.gcsText = (TextView) view.findViewById(R.id.gcsText);
                holder.jsyText = (TextView) view.findViewById(R.id.jsyText);
                holder.czyText = (TextView) view.findViewById(R.id.czyText);
                holder.zjyText = (TextView) view.findViewById(R.id.zjyText);
                view.setTag(holder);
            }else {
                holder= (ViewHolder) view.getTag();
            }

            holder.gcsText.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    getData(i+"","gcs");
                }
            });
            holder.jsyText.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    getData(i+"","jsy");
                }
            });
            holder.czyText.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    getData(i+"","czy");
                }
            });
            holder.zjyText.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    getData(i+"","zjy");
                }
            });
            holder.weizhiText.setText("位置"+(i+1));
            if(list.get(0).getData().get(i).getGcs()!=null){holder.gcsText.setBackgroundColor(Color.parseColor("#82AAFF"));
                holder.gcsText.setText("姓名:"+list.get(0).getData().get(i).getGcs().getName()+"\n\n"+
                    "体力:"+list.get(0).getData().get(i).getGcs().getPower());}
            if(list.get(0).getData().get(i).getJsy()!=null){holder.jsyText.setBackgroundColor(Color.parseColor("#82AAFF"));
                holder.jsyText.setText("姓名:"+list.get(0).getData().get(i).getJsy().getName()+"\n\n"+
                    "体力:"+list.get(0).getData().get(i).getJsy().getPower());}
            if(list.get(0).getData().get(i).getCzy()!=null){holder.czyText.setBackgroundColor(Color.parseColor("#82AAFF"));
                holder.czyText.setText("姓名:"+list.get(0).getData().get(i).getCzy().getName()+"\n\n"+
                    "体力:"+list.get(0).getData().get(i).getCzy().getPower());}
            if(list.get(0).getData().get(i).getZjy()!=null){holder.zjyText.setBackgroundColor(Color.parseColor("#82AAFF"));
                holder.zjyText.setText("姓名:"+list.get(0).getData().get(i).getZjy().getName()+"\n\n"+
                    "体力:"+list.get(0).getData().get(i).getZjy().getPower());}
            return view;
        }


    }
}
